﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace AppMVVM.Models
{
    public class Credentials : INotifyPropertyChanged
    {
        private string _account;
        private string _password;

        public Credentials()
        {
        }

        public string Account
        {
            get { return _account; }
            set
            {
                if (_account != value)
                {
                    _account = value;
                    OnPropertyChanged("Account");
                }
            }
        }

        public string Password
        {
            get
            {
                var a = new SHA256Managed();
                if (!String.IsNullOrEmpty(_password))
                    return Convert.ToBase64String(a.ComputeHash(new System.Text.UTF8Encoding().GetBytes(_password)));
                return null;
            }
            set
            {
                if (_password != value)
                {
                    _password = value;
                    OnPropertyChanged("Password");
                }
            }
        }

        public bool IsValid
        {
            get { return !String.IsNullOrEmpty(_account) && !String.IsNullOrEmpty(_password); }
        }

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
